gitPath='/home/apps_admin/sources/Dashboard/';
buildsPath='/home/apps_admin/sources/Dashboard/';
version="v1."$(date +%y%m%d);
publishVersion="${version}-$(date +%H%M)";

deplyType='--uat';
port=7040;  
branch='main';
branchName='';

# sit publish
function sit() {
  cd $gitPath;
  source /etc/profile &> /dev/null;

  env='Development';
  rm -rf "${buildsPath}wwwroot";

  # git switch branch and pull codes.
  git checkout . &> /dev/null;
  
  if git checkout $branch &> /dev/null; then
    echo "  switch to branch:${branch}...";
    git checkout -b $branch origin/$branch &> /dev/null;
    echo '  branch switch completed!';
  fi
  
  echo '  git pull...';
  git pull;
  echo '  git pull completed!';
  
  # vue builds
  echo '  app build&publish...';
  cd $buildsPath;

  # npm i &> /dev/null;
  npm run install:front &> /dev/null;
  npm run build &> /dev/null;

  chmod 777 -R $buildsPath;
  # echo "window.version = '${publishVersion}${branchName}';" > ./Statics/settings.js;
  echo '  app build&publish completed!';
  
  echo '  remove docker app and image...';
  docker rm -f `docker ps -a | grep $port | awk {'print $1'}` &> /dev/null;
  docker rmi -f dashboard_${branch,,}_$port &> /dev/null;
  echo '  remove docker app and image completed!';
  
  echo '  build docker image...';
  
  # docker builder and start app.
  cd $buildsPath;
  docker build -t dashboard_${branch,,}_$port . &> /dev/null;
  
  if [ $? -ne 0 ]; then
    echo "  docker image build error, please retry!";
    
    exit 1;
  fi
  
  echo '  build docker image completed!';
  echo "  docker app is runing: dashboard_${branch,,}_$port";
  docker run -e "ASPNETCORE_ENVIRONMENT=${env}" --restart=always --name=dashboard_${branch,,}_$port -p $port:80 -d dashboard_${branch,,}_$port &> /dev/null;
  
  if [ $? -eq 0 ]; then
    echo "update success, please visit: http://10.164.2.213:${port}/";
  fi
}

if [ $# -ge 1 ]; then
  case "${1,,}" in
    mar) branch='2022_Mar'; branchName=' / Mar'; port=7040;;
    *) branch='main'; branchName=''; port=7040;;
  esac

  if [[ "${1,,}" != "--"* ]]; then
    shift;
  fi
fi

if [[ "${1,,}" = "--"* ]]; then
  deplyType="${1,,}";
  
  shift;
  
  if [[ "${1,,}" = "v1."* ]]; then
    fullVersion="$1";
    
    shift;
  else
    counts=$(docker images | grep "10.164.2.213:4300/wfmobile/dashboard" | grep "$version" | awk '{print $3}' | wc -l);
    
    newImageId=$(docker images | grep dashboard_${branch,,}_${port} | awk '{print $3}');
    oldImageId=$(docker images | grep 10.164.2.213:4300/wfmobile/dashboard | grep $version.$counts | awk '{print $3}');
    
    fullVersion="$version.$counts";
  
    if [ "$newImageId" != "$oldImageId" ]; then
      counts=$[$counts + 1];
      fullVersion="$version.$counts";
      
      echo '  begin pull...';
      
      docker tag dashboard_${branch,,}_${port} 10.164.2.213:4300/wfmobile/dashboard:$fullVersion &> /dev/null;
      docker push 10.164.2.213:4300/wfmobile/dashboard:$fullVersion;
      
      echo "  pull completed, the version is:${fullVersion}";
    fi
  fi
  
  if [ -n "$deplyType" ]; then
    case $deplyType in
      --uat)
        echo "  begin deploy to uat...";
        sshpass -p 'C@pitaland123' ssh apps_admin@10.164.2.133 "sudo sh deploy_dashboard.sh ${fullVersion} $*";;
      --prd)
        echo "  begin deploy to production...";
        sshpass -p 'C@pitaland123' ssh apps_admin@10.164.5.160 "sudo sh deploy_dashboard.sh ${fullVersion} $*";;
      *)
      echo "  error:not current oprator: $deplyType!";;
    esac
  fi
else
  sit;
fi
